<?php

App::uses('AppController', 'Controller');

/**
 * CoreModules Controller
 *
 * @property CoreModule $CoreModule
 */
class CoreModulesController extends AppController {
    static $iconCls = "card";

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->CoreModule->recursive = 0;
        $this->set('coreModules', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->CoreModule->exists($id)) {
            throw new NotFoundException(__('Invalid core module'));
        }
        $options = array('conditions' => array('CoreModule.' . $this->CoreModule->primaryKey => $id));
        $this->set('coreModule', $this->CoreModule->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->CoreModule->create();
            if ($this->CoreModule->save($this->request->data)) {
                $this->Session->setFlash(__('The core module has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The core module could not be saved. Please, try again.'));
            }
        }
        $modulesArray = array_merge(array('pages'=>'Index'), $this->listModulesKeyAndValue());
        $this->set(compact('modulesArray'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->CoreModule->exists($id)) {
            throw new NotFoundException(__('Invalid core module'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->CoreModule->save($this->request->data)) {
                $this->Session->setFlash(__('The core module has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The core module could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('CoreModule.' . $this->CoreModule->primaryKey => $id));
            $this->request->data = $this->CoreModule->find('first', $options);
        }
        $modulesArray = array_merge(array('pages'=>'Index'), $this->listModulesKeyAndValue());
        $this->set(compact('modulesArray'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @throws MethodNotAllowedException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->CoreModule->id = $id;
        if (!$this->CoreModule->exists()) {
            throw new NotFoundException(__('Invalid core module'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->CoreModule->delete()) {
            $this->Session->setFlash(__('Core module deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Core module was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

    static $block_list_modules_iconCls = "cards-stack";
    public function block_list_modules() {
        $this->CoreModule->setBypass(true);
        $this->set('modules', $this->listActiveModulesFromDB());
        $this->CoreModule->setBypass(false);
    }

    public function listModules() {
        $dir = APP . "Controller";
        $files = scandir($dir);
        $modules = array();
        foreach ($files as $file) {
            preg_match('/^(.+)Controller\.php$/i', $file, $matches);
            if (!empty($matches) && $matches[1] != 'App' && $matches[1] != 'Pages') {
                $modules[] = $matches[1];
            }
        }
        return $modules;
    }

    public function listModulesKeyAndValue() {
        $listModules = $this->listModules();
        $newListModules = array();
        foreach ($listModules as $value) {
            $newListModules[$value] = $value;
        }
        return $newListModules;
    }

    public function listModulesFromDB() {
        $modules = $this->CoreModule->find('all');
        if(empty($modules))return array();
        $list = array();
        foreach ($modules as $value) {
            $list[$value["CoreModule"]["id"]] = $value["CoreModule"]["como_name"];
        }
        return $list;
    }
    
    public function listActiveModulesFromDB() {
        $options = array('conditions' => array('CoreModule.como_status' => 'a'));
        $modules = $this->CoreModule->find('all', $options);
        $list = array();
        foreach ($modules as $value) {
            $list[$value["CoreModule"]["id"]] = $value["CoreModule"]["como_name"];
        }
        return $list;
    }

    public function getModuleIdFromName($name) {
        $module = $this->getModuleFromName($name);
        return intval(@$module["CoreModule"]["id"]);
    }
    
    public function getModuleFromName($name) {
        $options = array('conditions' => array('CoreModule.como_name' => $name, 'CoreModule.como_status' => 'a'));
        $module = $this->CoreModule->find('first', $options);
        return $module;
    }
    
    public function getModuleFromNameWithOutStatus($name) {
        $options = array('conditions' => array('CoreModule.como_name' => $name));
        $module = $this->CoreModule->find('first', $options);
        return $module;
    }
    
    static $block_manage_modules_iconCls = "card--pencil";
    public function block_manage_modules(){
        $listModulesDb = $this->listModulesFromDB();
        $this->set(compact('listModulesDb'));
        
        $listModules = $this->listModules();
        $this->set(compact('listModules'));
        
    }
    
    public function toggleModule($comoName=null){
        $module = $this->getModuleFromNameWithOutStatus($comoName);
        if(empty($module)){
            $module = array();
            $module["CoreModule"]["como_name"]=$comoName;
            $module["CoreModule"]["como_status"]='a';
        }else{
            if($module["CoreModule"]["como_status"]=='a'){
                $module["CoreModule"]["como_status"]='i';
            }else{
                $module["CoreModule"]["como_status"]='a';
            }
        }
        $this->CoreModule->save($module["CoreModule"]);
        $this->layout = 'ajax';
        $this->set('result', $module["CoreModule"]["como_status"]);
    }
    
    
    static $block_context_menu_iconCls = "cursor";
    public function block_context_menu() {
        $this->CoreModule->setBypass(true);
        $modules = $this->listActiveModulesFromDB();
        App::import('Controller', 'CoreBlocks');
        $coreBlock = new CoreBlocksController();
        foreach ($modules as $key => $value) {
            $modules[$key] = array('name'=>$value, 'blocks'=>$coreBlock->listBlocks($value));
        }
        
        $this->set('modules', $modules);
        $this->CoreModule->setBypass(false);
    }
    
    static $block_accordion_modules_iconCls = "ui-accordion";
    public function block_accordion_modules() {
        $this->CoreModule->setBypass(true);
        $modules = $this->listActiveModulesFromDB();
        App::import('Controller', 'CoreBlocks');
        $coreBlock = new CoreBlocksController();
        foreach ($modules as $key => $value) {
            $schema = null;
            try {
                if(file_exists(APP.DS."Controller".DS."{$value}Controller.php")){
                    App::import('Controller', $value);
                    eval("\$object = new {$value}Controller();");
                    eval("\$schema = \$object->{$object->modelClass}->schema();");
                }
            } catch (Exception $e) {
                
            }
            $modules[$key] = array('name'=>$value, 'blocks'=>$coreBlock->listBlocks($value), 'model'=>!empty($schema));
        }
        
        $this->set('modules', $modules);
        $this->CoreModule->setBypass(false);
    }

}
